//
//  ZQQBannerView.m
//  ZQQBannerView
//
//  Created by MoDao-iOS on 2017/7/15.
//  Copyright © 2017年 wushengdubai. All rights reserved.
//

#import "ZQQBannerView.h"

static NSInteger const radio = 10;

@interface ZQQBannerView()<UIScrollViewDelegate>
{
    NSInteger _currentPage;
}

/**
 *  记录着根据模型数组, 添加的imageView控件
 */
@property (nonatomic, strong) NSMutableArray <UIImageView *>*adPics;
/**
 *  存放图片的内容视图
 */
@property (nonatomic, strong) UIScrollView *contentView;
/**
 *  页码指示
 */
@property (nonatomic, strong) UIPageControl *pageControl;
/**
 *  自动滚动的timer
 */
@property (nonatomic, strong) NSTimer *scrollTimer;

@end

@implementation ZQQBannerView

+ (instancetype)bannerViewWithLoadImageBlock:(LoadImageBlock)loadBlock withPagePosition:(BannerViewPageControlPosition)position {
    ZQQBannerView *bannerV = [[ZQQBannerView alloc] init];
    bannerV.loadBlock = loadBlock;
    bannerV.pageControlPosition = position;
    return bannerV;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.contentView.frame = self.bounds;
    self.pageControl.frame = CGRectMake(0, self.frame.size.height - 20, self.frame.size.width, 20);
    [self pageControlSizeFit];
    
    NSInteger count = self.adPics.count;
    CGFloat width = self.contentView.frame.size.width;
    CGFloat height = self.contentView.frame.size.height;
    for (int i = 0; i < count; i++) {
        UIImageView *imgV = self.adPics[i];
        imgV.frame = CGRectMake(i * width, 0, width, height);
    }
    
    self.contentView.contentSize = CGSizeMake(width * count, 0);
    [self scrollViewDidEndDecelerating:self.contentView];
}

- (void)setPicModels:(NSArray<id<ZQQBannerProtocol>> *)picModels {
    _picModels = picModels;
    
    // 1. 移除之前的控件
    [self.adPics makeObjectsPerformSelector:@selector(removeFromSuperview)];
    self.adPics = nil;
    
    // 2. 根据模型添加新的控件
    NSInteger baseCount = picModels.count;
    NSInteger count = baseCount;
    if (baseCount > 1) {
        count = baseCount *radio;
    }
    for (int i = 0 ;i < count; i++) {
        id<ZQQBannerProtocol> picM = picModels[i % baseCount];
        // 1. 创建控件
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.tag = self.adPics.count;
        
        // 2. 设置图片
        if (self.loadBlock) {
            self.loadBlock(imageView, picM.adImgURL);
        }
        
        // 3.添加手势，点击图片跳转
        UITapGestureRecognizer *tag = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(jumpToLink:)];
        imageView.userInteractionEnabled = YES;
        [imageView addGestureRecognizer:tag];
        
        // 4.添加到父控件，以及使用数组保存
        [self.contentView addSubview:imageView];
        [self.adPics addObject:imageView];
    }
    
    self.pageControl.numberOfPages = picModels.count;
    
    [self setNeedsLayout];
    if (picModels.count > 1) {
        [self.scrollTimer fire];
    } else {
        [self.scrollTimer invalidate];
        self.scrollTimer = nil;
    }
    
}

/**
 图片点击方法

 @param gester tap手势
 */
- (void)jumpToLink:(UITapGestureRecognizer *)gester {
    UIView *imageView = gester.view;
    NSInteger tag = imageView.tag % self.picModels.count;
    id<ZQQBannerProtocol> adM = self.picModels[tag];
    
    
    if (adM.clickImgBlock != nil) {
        adM.clickImgBlock();
    }
}

/**
 定时器方法
 */
- (void)autoScrollNextPage {
    NSInteger page = _currentPage + 1;
    [self.contentView setContentOffset:CGPointMake(self.contentView.frame.size.width * page, 0) animated:YES];
}

#pragma mark ====UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self.scrollTimer invalidate];
    self.scrollTimer = nil;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (self.adPics.count > 1) {
        [self scrollTimer];
    }
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    [self caculateCurrentPage:scrollView];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self caculateCurrentPage:scrollView];
}

- (void)caculateCurrentPage: (UIScrollView *)scrollView  {
    if (self.picModels.count == 0) {
        return;
    }
    if (self.picModels.count == 1) {
        _currentPage = 1;
        if ([self.delegate respondsToSelector:@selector(adBannerViewDidSelectedPicModel:)]) {
            [self.delegate adBannerViewDidSelectedPicModel:self.picModels[self.pageControl.currentPage]];
        }
        return;
    }
    // 确认中间区域
    NSInteger min = self.picModels.count * (radio / 2);
    NSInteger max = self.picModels.count * (radio / 2 + 1);
    
    NSInteger page = scrollView.contentOffset.x / scrollView.frame.size.width;
    self.pageControl.currentPage = page % self.picModels.count;
    
    if (page < min || page > max) {
        page = min + page % self.picModels.count;
        [scrollView setContentOffset:CGPointMake(page * scrollView.frame.size.width, 0)];
    }
    
    _currentPage = page;
    if ([self.delegate respondsToSelector:@selector(adBannerViewDidSelectedPicModel:)]) {
        [self.delegate adBannerViewDidSelectedPicModel:self.picModels[self.pageControl.currentPage]];
    }
}

 #pragma mark ====懒加载
-(NSTimer *)scrollTimer {
    if (_scrollTimer == nil) {
        _scrollTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(autoScrollNextPage) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:_scrollTimer forMode:NSRunLoopCommonModes];
    }
    return _scrollTimer;
}

-(UIPageControl *)pageControl {
    if (_pageControl == nil) {
        UIPageControl *pageControl = [[UIPageControl alloc] init];
        pageControl.pageIndicatorTintColor = [UIColor grayColor];
        pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
        pageControl.hidesForSinglePage = YES;
        [self addSubview:pageControl];
        _pageControl = pageControl;
    }
    return _pageControl;
}

- (void)setPageControlPosition:(BannerViewPageControlPosition)pageControlPosition {
    _pageControlPosition = pageControlPosition;
    [self pageControlSizeFit];
}

- (void)pageControlSizeFit {
    [self.contentView bringSubviewToFront:self.pageControl];
    CGSize pointSize = [self.pageControl sizeForNumberOfPages:self.picModels.count];
    CGFloat margin = pointSize.width / self.picModels.count;
    CGFloat page_x = ((self.pageControl.bounds.size.width - pointSize.width) / 2 - margin);
    
    if (self.pageControlPosition == BannerViewPageControlPositionLeft) {
        [self.pageControl setBounds:CGRectMake(page_x, self.pageControl.bounds.origin.y, self.bounds.size.width, 20)];
    } else if (self.pageControlPosition == BannerViewPageControlPositionRight) {
        [self.pageControl setBounds:CGRectMake(- page_x, self.pageControl.bounds.origin.y, self.bounds.size.width, 20)];
    } else {
        
    }
}

-(UIScrollView *)contentView {
    if (_contentView == nil) {
        UIScrollView *contentView = [[UIScrollView alloc] init];
        contentView.pagingEnabled = YES;
        contentView.showsHorizontalScrollIndicator = NO;
        _contentView = contentView;
        _contentView.delegate = self;
        [self addSubview:contentView];
    }
    return _contentView;
}

-(NSMutableArray<UIImageView *> *)adPics
{
    if (_adPics == nil) {
        _adPics = [NSMutableArray array];
    }
    return _adPics;
}

@end
